package com.neusoft.controller;

import com.neusoft.entity.Rent;
import com.neusoft.entity.common.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.util.*;


@Controller
@RequestMapping("rentConsumer")
public class RentConsumerController {
    @Autowired
    private RestTemplate restTemplate;

    private String url_prefix = "http://localhost:8001";
//      private String url_prefix = "http://provider-neusoft";

    @PostMapping("save")
    @ResponseBody
    public void save(Rent bean) {
        String url = url_prefix + "/rent/save";
        System.out.println(bean);
        restTemplate.put(url, bean);
    }

    @PostMapping("queryAll")
    @ResponseBody
    public Object queryAll(Integer page, Integer limit,Rent bean) {
        System.out.println("bean值为："+bean);
        System.out.println("page值为："+page);
        System.out.println("limit值为："+limit);
        String url = url_prefix + "/rent/queryAll/"+page+"/"+limit;
        return restTemplate.postForObject(url,bean,CommonResult.class);
    }

    @RequestMapping("queryById/{id}")
    @ResponseBody
    public Object queryById(@PathVariable Integer id) {
        String url = url_prefix + "/rent/queryById/" + id;
        return restTemplate.getForObject(url, Rent.class);
    }

    @PostMapping("deleteById")
    @ResponseBody
    public ResponseEntity<Boolean> deleteById(Integer[] ids){
        for(int i=0;i<ids.length;i++) {
            System.out.println("ids值为：" + ids[i]);
        }
        StringBuffer sb = new StringBuffer();
        System.out.println(Arrays.toString(ids));
        sb.append(Arrays.toString(ids));
        sb.deleteCharAt(sb.indexOf("["));
        sb.deleteCharAt(sb.lastIndexOf("]"));
        System.out.println(sb.toString());
        String url = url_prefix+"/rent/deleteById/" + sb.toString();
        return restTemplate.postForEntity(url,sb.toString(),Boolean.class);
    }

    @PostMapping("deleteById1/{id}")
    @ResponseBody
    public ResponseEntity<Boolean> deleteById1(@PathVariable Integer id){
        String url = url_prefix+"/rent/deleteById1/" + id;
        return restTemplate.postForEntity(url,id,Boolean.class);
    }

    @PostMapping("checkin/{id}")
    @ResponseBody
    public void checkin(@PathVariable  Integer id){
        String url = url_prefix+"/rent/checkin/"+id;
        restTemplate.put(url, id);
    }

    @RequestMapping("toList")
    public String toList() {
        return "user/rent_list";
    }

    @RequestMapping("toEdit")
    public String toEdit(Integer id, Model model){
        Rent bean = (Rent) queryById(id);
        model.addAttribute("bean",bean);
        return "user/rent_add";
    }

    @RequestMapping("toAdd")
    public String toAdd(Model model){
        model.addAttribute("bean",new Rent());
        return "user/rent_add";
    }

}
